package doc.ic.walkmore;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import doc.ic.walkmore.DAO.StepsDataSource;
import doc.ic.walkmore.DAO.model.Steps;
import doc.ic.walkmore.GUI.GraphView;
import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GraphActivity extends Activity {
	
	private StepsDataSource datasource;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		
		// Get date from the extra (added when activity was launched)
		String date = getIntent().getStringExtra("date");
		
		
        // DataSource
        datasource = new StepsDataSource(this);
        datasource.open();

		HashMap<String, Long> steps = datasource.getTotalStepsByHour(date);
		
		
		float[] values = new float[24];
		Long subtotal = 0L;
		for (int i=0; i<24; i++) {
			Long val = steps.get(String.format("%02d",i));
			if (val!= null) {
				subtotal += val;
			}
			values[i] = subtotal ;
		}
//		for(String step : steps.keySet() ) {
//			System.out.println(step);
//		}
		
		String[] horLabels = {"0h", "4h", "8h", "12h", "16h", "20h", "24h"};
		String[] verLabels = {String.format("%d",subtotal), "0"};
		GraphView myView = new GraphView(this, values, "Daily graph", horLabels, verLabels, false);
		
		setContentView(R.layout.daily_graph);
		LinearLayout mainView = (LinearLayout) findViewById(R.id.graphViewLayout);
		TextView tvdate = (new TextView(this));
		tvdate.setText(date);
		tvdate.setTextSize(32);
		mainView.addView(tvdate);
		mainView.addView(myView);
	}
	
	/**
	 * onClickListener button back
	 * @param the button clicked
	 */
	public void stopActivity(View v) {
		onPause();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		datasource.close();
		finish();
	}

}
